package gamagora.game.world.maps.game
{
	import gamagora.game.entity.OSBridge;
	import gamagora.game.entity.OSEntity;
	import gamagora.game.entity.OSGround;
	import gamagora.game.entity.OSMovingBox;
	import gamagora.game.entity.OSPillar;
	import gamagora.game.entity.OSSari;
	import gamagora.game.resources.OSImages;
	import gamagora.game.states.OSEndJungleState;
	import gamagora.game.states.OSEndWomanState;
	import gamagora.game.world.maps.OSMap;
	
	import org.flixel.FlxG;
	
	public class OSMapEnd extends OSMap
	{
		private var _leftBlock:OSGround;
		private var _rightBlock:OSGround;
		private var _sari:OSSari;
		private var _stopScroll:Boolean;
		private var _pickupSari:Boolean;
		private var _end:Boolean;
		
		public function OSMapEnd()
		{
			super(OSImages.JUNGLE);
			
			addForeground(new OSEntity(NOTHING, 0, 0, OSImages.END_TREE));
			
			var ground:OSGround = new OSGround(0, 760, 560, 20);
			ground.setNoErosionLeft([0]);
			ground.setNoErosionUp([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]);
			ground.setNoErosionRight([27]);
			ground.erode();
			add(ground);
			
			addForeground(new OSPillar(100, 760, 20, 300));
			addForeground(new OSPillar(510, 760, 20, 300));
			
			ground = new OSGround(240, 680, 240, 80);
			ground.setNoErosionDown([36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]);
			ground.erode();
			add(ground);
			
			add(new OSBridge(560, 770, 20, 10, true));
			
			_sari = new OSSari(580, 490, 0);
			_sari.angle = -60;
			addForeground(_sari);
			
			_leftBlock = new OSGround(-20, 0, 20, HEIGHT);
			_rightBlock = new OSGround(WIDTH, 0, 20, HEIGHT);
		}
		
		public override function update():void
		{
			super.update();
			if(!_stopScroll && inside(_world.getPlayerX()) && _world.getPlayerX() % OSMap.WIDTH > OSMap.WIDTH * 0.5 + 50)
			{
				FlxG.camera.follow(null);
				_leftBlock.world = _world;
				_rightBlock.world = _world;
				addEntityToWorld(_leftBlock);
				addEntityToWorld(_rightBlock);
				_stopScroll = true;
				_world.stopBackgroundScroll();
			}
			else
			{
				if(!_pickupSari)
				{
					if(_world.getPlayerX() >= _sari.x && _world.getPlayerX() <= _sari.x + 20 && _world.getPlayerY() < 600)
					{
						_pickupSari = true;
						_sari.hide = true;
						removeEntityFromWorld(_leftBlock);
						removeEntityFromWorld(_rightBlock);
					}
				}
				else 
				{
					if(!_end)
					{
						if(_world.getPlayerX() < x)
						{
							FlxG.fade(0xff000000, 3, goToWoman);
							_world.hidePlayer();
							_end = true;
						}
						else
						{
							if(_world.getPlayerX() > x + OSMap.WIDTH)
							{
								FlxG.fade(0xff000000, 3, goToJungle);
								_world.hidePlayer();
								_end = true;
							}
						}
					}
				}
			}
		}
		
		public override function setStartX(decalX:Number):void
		{
			super.setStartX(decalX);
			_leftBlock.setStartX(x);
			_rightBlock.setStartX(x);
		}
		
		public override function destroy():void
		{
			super.destroy();
			_leftBlock.destroy();
			_rightBlock.destroy();
		}

		private function goToWoman():void
		{
			FlxG.switchState(new OSEndWomanState());
		}
		
		private function goToJungle():void
		{
			FlxG.switchState(new OSEndJungleState());
		}
	}
}
